package com.shire.movienizerdroid.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.shire.movienizerdroid.activity.MDPreferenceActivity;

/**
 * @author yhernandezdiaz
 */
public abstract class Utils {
	public static final String COVERS_FOLDER = "/Covers/";
	public static final String COVERS_ORANGE_THUMBS_FOLDER = "/Covers/ThumbsOrangeCovers/";
	public static final String COVERS_DROID_THUMBS_FOLDER = "/Covers/ThumbsDroidCovers/";

	public static final String getCoverUrl(Context context, String imagePath, String imageUrl) {
		if ((!TextUtils.isEmpty(imagePath))) {
			final String basePath = MDPreferenceActivity.getDatabaseParentPath(context);
			File file;
			if ((file = new File(String.format("%s%s%s", basePath, COVERS_FOLDER, imagePath))).exists())
				return "file://" + file.getAbsolutePath();
		}
		if ((!TextUtils.isEmpty(imageUrl)) && (MDPreferenceActivity.isDownloadingPicturesAllowed(context)))
			return imageUrl;
		else
			return null;
	}

	public static final String getCoverThumbnailUrl(Context context, String imagePath, String imageUrl) {
		if ((!TextUtils.isEmpty(imagePath))) {
			final String basePath = MDPreferenceActivity.getDatabaseParentPath(context);
			File file;
			if ((file = new File(String.format("%s%s%s", basePath, COVERS_DROID_THUMBS_FOLDER, imagePath))).exists())
				return "file://" + file.getAbsolutePath();
			else if ((file = new File(String.format("%s%s%s", basePath, COVERS_ORANGE_THUMBS_FOLDER, imagePath))).exists())
				return "file://" + file.getAbsolutePath();
			else if ((file = new File(String.format("%s%s%s", basePath, COVERS_FOLDER, imagePath))).exists())
				return "file://" + file.getAbsolutePath();
		}
		if ((!TextUtils.isEmpty(imageUrl)) && (MDPreferenceActivity.isDownloadingPicturesAllowed(context)))
			return imageUrl;
		else
			return null;
	}

	public static final String getCoverPath(Context context, String imagePath, String imageUrl) {
		if ((!TextUtils.isEmpty(imagePath))) {
			final String basePath = MDPreferenceActivity.getDatabaseParentPath(context);
			File file;
			if ((file = new File(String.format("%s%s%s", basePath, COVERS_FOLDER, imagePath))).exists())
				return file.getAbsolutePath();
		}
		return null;
	}

	public static final Object[] getCoverThumbnailInfo(Context context, String imagePath, String imageUrl) {
		if ((!TextUtils.isEmpty(imagePath))) {
			final String basePath = MDPreferenceActivity.getDatabaseParentPath(context);
			File file;
			if ((file = new File(String.format("%s%s%s", basePath, COVERS_DROID_THUMBS_FOLDER, imagePath))).exists())
				return new Object[] { file.getAbsolutePath(), false };
			else if ((file = new File(String.format("%s%s%s", basePath, COVERS_ORANGE_THUMBS_FOLDER, imagePath))).exists())
				return new Object[] { file.getAbsolutePath(), false };
			else if ((file = new File(String.format("%s%s%s", basePath, COVERS_FOLDER, imagePath))).exists())
				return new Object[] { file.getAbsolutePath(), true };
		}
		return null;
	}

	public static final Bitmap generateCoverThumbnail(String imagePath) {
		final BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imagePath, opts);
		opts.inJustDecodeBounds = false;
		opts.inPurgeable = true;
		opts.inSampleSize = Math.max(opts.outWidth / 50, opts.outHeight / 100);
		final Bitmap bitmap = BitmapFactory.decodeFile(imagePath, opts);
		try {
			final File f = new File(imagePath.replace(COVERS_FOLDER, COVERS_DROID_THUMBS_FOLDER).replace('\\', '/'));
			f.getParentFile().mkdirs();
			final FileOutputStream fout = new FileOutputStream(f);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fout);
			fout.close();
		} catch (FileNotFoundException e) {} catch (IOException e) {}
		return bitmap;
	}

	private Utils() {}

}
